﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServisniList.Core;

namespace ServisniList
{
    public partial class OpisRada : Form
    {

        #region Fields

        private DataModelDataContext context = null;

        #endregion

        #region Constructor

        public OpisRada()
        {
            InitializeComponent();
        }

        #endregion

        #region Event handlers

        private void OpisRada_Load(object sender, EventArgs e)
        {
            context = new DataModelDataContext(ConfigurationManager.ConnectionStrings["ServisniListConnectionString"].ConnectionString);
            opisRadaBindingSource.DataSource = context.OpisRadas.ToList();
        }

        private void btnSnimiPodatke_Click(object sender, EventArgs e)
        {
            try
            {
                this.Update();
                context.SubmitChanges();
            }
            catch (Exception err)
            {
                if (err.Message.Contains("OpisRadaOpisUnique"))
                {
                    MessageBox.Show("Promijene nije moguće unijeti stvara se dupli opis rada !");
                }
            }
        }

        private void btnDodajNovi_Click(object sender, EventArgs e)
        {
            OpisRadaNovi noviOpis = new OpisRadaNovi();
            noviOpis.ShowDialog();

            if (noviOpis.UspjesnoKreiranNovi)
            {
                opisRadaBindingSource.DataSource = context.OpisRadas.ToList();
            }

        }

        private void OpisRada_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (context != null)
            {
                context.Dispose();
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }

        #endregion

    }
}
